package com.taikang.hmp.frame.user.action.view;

import java.util.List;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.taikang.hmp.frame.common.TKException;
import com.taikang.hmp.frame.common.datastructre.Dto;
import com.taikang.hmp.frame.common.datastructre.support.BaseDto;
import com.taikang.hmp.frame.common.datastructre.support.ResponseDto;
import com.taikang.hmp.frame.common.web.action.BaseAction;
import com.taikang.hmp.frame.user.service.IRoleManagementService;

/** 
 * 角色管理与授权
 * author:qrp
 * time:2015年11月9日 上午10:41:12
 */
public class RoleManagementAction extends BaseAction{
	private static final long serialVersionUID = 1L;
	private static Logger logger = LoggerFactory.getLogger(RoleManagementAction.class);
	
	private IRoleManagementService roleManagementService;
	

	public IRoleManagementService getRoleManagementService() {
		return roleManagementService;
	}

	public void setRoleManagementService(
			IRoleManagementService roleManagementService) {
		this.roleManagementService = roleManagementService;
	}

	public String executeAction(){
		if("getAllRole".equals(requestDto.getMethod())){
			logger.info("--【获取所有权限】 method:getAllRole -- ");
			return getAllRole();
		}else if("addRole".equals(requestDto.getMethod())){
			logger.info("--【获取所有权限节点信息】 method:addRole -- ");
			return addRole();
		}else if("delRole".equals(requestDto.getMethod())){
			logger.info("--【删除角色】 method:delRole -- ");
			return delRole();
		}else if("editRolePer".equals(requestDto.getMethod())){
			logger.info("--【编辑角色权限】 method:editRolePer -- ");
			return editRolePer();
		}else if("saveRolePer".equals(requestDto.getMethod())){
			logger.info("--【保存新增角色权限】 method:saveRolePer -- ");
			return saveRolePer();
		}else if("saveEditRolePer".equals(requestDto.getMethod())){
			logger.info("--【保存编辑后权限信息】 method:saveEditRolePer -- ");
			return saveEditRolePer();
		}else{
			throw new TKException("no support method:"+requestDto.getMethod());
		}
	}
	
	
	/**
	 * 取得所有权限列表
	 * @return
	 */
	public String getAllRole(){
		ResponseDto responseDto = new ResponseDto();
		
		List<Dto> resultList = roleManagementService.getAllRole();
		
		Dto resultDto = new BaseDto();
		resultDto.put("resultlist", resultList);
		responseDto.pushResultDto(resultDto);
		super.setDataMap(responseDto);
		
		return SUCCESS;
	}
	/**
	 * 新增角色
	 * @return
	 */
	public String addRole(){
		ResponseDto responseDto = new ResponseDto();
		
		List<Dto> resultList = roleManagementService.getPermission();
		
		Dto resultDto = new BaseDto();
		resultDto.put("resultlist", resultList);
		responseDto.pushResultDto(resultDto);
		super.setDataMap(responseDto);
		
		return SUCCESS;
	}
	/**
	 * 删除角色
	 * @return
	 */
	public String delRole(){
		ResponseDto responseDto = new ResponseDto();
		//获取参数
		Dto paramDto = requestDto.getRequestlist().get(0);
		paramDto = super.appendRequestBase(paramDto, requestDto);
		if(roleManagementService.queryRoleUser(paramDto)){
			roleManagementService.delRole(paramDto);
			return getAllRole();
		}else{
			responseDto.addErrorCode("此角色下仍有关联用户！");
			super.setDataMap(responseDto);
			return SUCCESS;
		}
	}
	/**
	 * 获得要编辑角色的权限
	 * @return
	 */
	public String editRolePer(){
		ResponseDto responseDto = new ResponseDto();
		//获取参数
		Dto paramDto = requestDto.getRequestlist().get(0);
		paramDto = super.appendRequestBase(paramDto, requestDto);
		String roleId = paramDto.getAsString("roleId");
		
		List<Dto> resultList = roleManagementService.getPermissionByRole(roleId);
		
		Dto resultDto = new BaseDto();
		resultDto.put("resultlist", resultList);
		resultDto.put("roleId", roleId);
		responseDto.pushResultDto(resultDto);
		super.setDataMap(responseDto);
		return SUCCESS;
	}
	/**
	 * 保存新增角色权限
	 * @return
	 */
	public String saveRolePer(){
		ResponseDto responseDto = new ResponseDto();
		//获取参数
		Dto paramDto = requestDto.getRequestlist().get(0);
		paramDto = super.appendRequestBase(paramDto, requestDto);
		
		boolean message = false;
		Boolean temp = roleManagementService.isExistRoleName(paramDto);
		if(temp){
		    message = true;
		}else{
			roleManagementService.savePermission(paramDto);
		}
		
		Dto resultDto = new BaseDto();
		resultDto.put("message", message);
		responseDto.pushResultDto(resultDto);
		super.setDataMap(responseDto);
		
		return SUCCESS;
	}
	/**
	 * 保存编辑后的角色权限
	 * @return
	 */
	public String saveEditRolePer(){
		//获取参数
		Dto paramDto = requestDto.getRequestlist().get(0);
		paramDto = super.appendRequestBase(paramDto, requestDto);
		
		roleManagementService.saveEditPermission(paramDto);
		return SUCCESS;
	}

}
